package com.glory.eapequipmentmonitor.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.bean.ThingBean;
import com.glory.eapequipmentmonitor.callback.DeleteCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class EqpThingListAdapter extends BaseQuickAdapter<ThingBean, BaseViewHolder> {
    private DeleteCallback deleteCallback;
    private FragmentManager fragmentManager;
    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;

    public EqpThingListAdapter(@Nullable List<ThingBean> data, DeleteCallback deleteCallback) {
        super(R.layout.adapter_eqp_thing_list, data);
        this.deleteCallback = deleteCallback;
    }


    public void setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final ThingBean item) {
        helper.setText(R.id.tvTitle, item.getSEQNO());
        helper.setText(R.id.tvThingCode, item.getMATCODE());
        helper.setText(R.id.tvThingName, item.getMLOTID());
        helper.getView(R.id.btnChange).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
                View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_scan_thing, null);
                TextView tvCancel = view.findViewById(R.id.tvCancel);
                final FreeUI_ClearEditText etThingCode = view.findViewById(R.id.etThingCode);
                final FreeUI_ClearEditText etThingId = view.findViewById(R.id.etThingId);
                etThingCode.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (start == 0 && before == 0 && count > 1) {
                            if (etThingId.getText().toString().trim().length() > 0) {
                                item.setMATCODE(etThingCode.getText().toString().trim());
                                item.setMLOTID(etThingId.getText().toString().trim());
                                notifyDataSetChanged();
                                FreeApi_ToastUtils.showFreeToast("更换成功", mContext, true, Toast.LENGTH_SHORT);
                                freeUI_generalFragmentDialog.dismiss();
                            } else {
                                etThingId.requestFocus();
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                etThingId.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (start == 0 && before == 0 && count > 1) {
                            if (etThingCode.getText().toString().trim().length() > 0) {
                                item.setMATCODE(etThingCode.getText().toString().trim());
                                item.setMLOTID(etThingId.getText().toString().trim());
                                notifyDataSetChanged();
                                FreeApi_ToastUtils.showFreeToast("更换成功", mContext, true, Toast.LENGTH_SHORT);
                                freeUI_generalFragmentDialog.dismiss();
                            } else {
                                etThingCode.requestFocus();
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        freeUI_generalFragmentDialog.dismiss();
                    }
                });
                freeUI_generalFragmentDialog.setLayout(view);
                freeUI_generalFragmentDialog.show(fragmentManager, "11");
            }
        });
        helper.getView(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                freeUI_generalFragmentDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(mContext, "确定要删除吗？",
                        new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                freeUI_generalFragmentDialog.dismiss();
                                deleteCallback.onSuccess(item);
                            }
                        }, new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                freeUI_generalFragmentDialog.dismiss();
                            }
                        });
                freeUI_generalFragmentDialog.show(fragmentManager, "freeUI_generalFragmentDialog");
            }
        });
    }
}
